package com.codeOfDoom.Sets.SetsBoard
{
	import com.codeOfDoom.Sets.Managers.DeckManager.IDeckManager;
	import com.codeOfDoom.Sets.SetsCard.ICard;
	import com.codeOfDoom.Sets.SetsCard.SetsCard;
	
	import mx.collections.ArrayCollection;
	
	public class SetsBoard 
	{
		private var _deckManager:IDeckManager;
		private var _visibleCards:ArrayCollection;
		private static const INITIAL_CARDS_TO_PLAY:int = 12; 
		private static const REPLACEMENT_CARDS:int = 3;
		private var _fillerCards:int=0;
		public function SetsBoard(deckManager:IDeckManager)
		{
			_deckManager = deckManager;
			_visibleCards = _deckManager.replaceCards(INITIAL_CARDS_TO_PLAY);
		}
		public function get visibleCards():ArrayCollection{
			return _visibleCards;
		}
		public function get fillerCards():int{
			return _fillerCards;
		}
		public function getReplacementCards(oldCards:ArrayCollection):void{
			//should be using ReplaceSetEvent. im tired and i just want this to work before  i go to sleep
			var replacementCards:ArrayCollection;
			replacementCards = _deckManager.replaceCards(REPLACEMENT_CARDS - getTotalOfFillerCardsUsedInSet(oldCards));
			for each (var oldCard:SetsCard in oldCards){
				var replaceIndex:int = visibleCards.getItemIndex(oldCard);
				visibleCards.removeItemAt(replaceIndex);
				if (oldCard.isReplaceCard)
					_fillerCards--;
				//dont replace filler cards.
				if (replacementCards.length > 0 && !oldCard.isReplaceCard){
					visibleCards.addItemAt(replacementCards.getItemAt(0),replaceIndex);
					replacementCards.removeItemAt(0);
				}
			}
		}
		public function addCard(totalCardsToAdd:int):void{
			var newCards:ArrayCollection = _deckManager.replaceCards(totalCardsToAdd);
			_fillerCards+=totalCardsToAdd;
			for each (var setsCard:SetsCard in newCards){
				setsCard.isReplaceCard=true;
				visibleCards.addItem(setsCard);
			}
		}
		
		public function getTotalOfFillerCardsUsedInSet(oldCards:ArrayCollection):int{
			var count:int=0;
			for each(var setsCard:SetsCard in oldCards){
				if (setsCard.isReplaceCard)
					count++;
			}
			return count;
				
		}
		
		public function getVisibleCardsAsSetCards():ArrayCollection{
			var cards:ArrayCollection = new ArrayCollection();
			for each (var card:ICard in visibleCards){
				var setsCard:SetsCard = new SetsCard(card);
				cards.addItem(setsCard);
			} 
			return cards;
		}

	}
}